import 'package:aio/aio.dart';

///班组
class MyGroupBcInfo extends RollerBean {
  String IsTakePic = ""; //": 1, 是否需要拍照；0不需要拍照
  String GroupId = ""; //": "2", 班组id
  String GroupName = ""; //": "切割乙班" 班组名称
  List<MyAttendances>? Attendances =
      null; //班次信息;如果只有一个班次，说明该该班次系统已经选好，用户无法再主动选择。

  //打卡是否需要拍照
  bool isTakePic() {
    if ("1" == IsTakePic) {
      return true;
    }
    return false;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    IsTakePic = json["IsTakePic"]?.toString() ?? "";
    GroupId = json["GroupId"];
    GroupName = json["GroupName"];
    Attendances = setGenericFromJson(
        json, 'Attendances', <MyAttendances>[], MyAttendances());
    key = GroupId;
    value = GroupName;
  }

  @override
  instance() {
    return MyGroupBcInfo();
  }

  @override
  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["IsTakePic"] = IsTakePic;
    map["GroupId"] = GroupId;
    map["GroupName"] = GroupName;
    map["Attendances"] = Attendances;
    return map;
  }
}

///班次
// "Attendance": {
// "AttendanceId": 0,
// "WorkTypeName": null,
// "OnWorkTime": "2022/8/30 8:00:00",
// "OffWorkTime": "2022/8/30 17:30:00"
// },
class MyAttendances extends BaseBean {
  String AttendanceId = ""; //": 4, id
  String WorkTypeName = ""; //": "晚班", 班次名称
  String OnWorkTime = ""; //": "18:30", 上班开始时间
  String OffWorkTime = ""; //": "02:30" 下班时间

  String? _key = null;

  ///fixme 获取班次唯一标志; id可能会有相同的。如：今天的早班和明天的早班。两个相同的早班。但是时间是不同的。
  String getAttendanceKey() {
    if (isEmpty(_key)) {
      _key = AttendanceId + WorkTypeName + OnWorkTime + OffWorkTime;
    }
    return _key ?? "";
  }

  String? _WorkTypeNameAll = null;

  ///fixme 获取班次名称
  String getWorkTypeNameAll() {
    if (_WorkTypeNameAll == null) {
      if (OnWorkTime.length > 10 && OffWorkTime.length > 10) {
        _WorkTypeNameAll = WorkTypeName +
            "(" +
            formatOnWorkTime(format: "MM-dd") +
            ")" +
            "\n" +
            formatOnWorkTime(format: "HH:mm") +
            "~" +
            formatOffWorkTime(format: "HH:mm");
      } else {
        _WorkTypeNameAll = WorkTypeName + "\n" + OnWorkTime + "~" + OffWorkTime;
      }
    }
    return _WorkTypeNameAll ?? "";
  }

  DateTime? _OnWorkTime = null;

  _onWorkTime() {
    if (_OnWorkTime == null && isNotEmpty(OnWorkTime)) {
      try {
        String? OnWorkTime2 = OnWorkTime?.replaceAll("/", "-");
        _OnWorkTime = DateTimeUtils.stringToDateTime(OnWorkTime2,
            format: "yyyy-MM-dd HH:mm");
      } catch (e) {
        print("班次上班时间_onWorkTime()方法解析异常：$e");
        // try {
        //   String? OnWorkTime2 = OnWorkTime?.replaceAll("/", "-");
        //   _OnWorkTime = DateTimeUtils.stringToDateTime(OnWorkTime2,
        //       format: "yyyy-MM-dd HH:mm");
        // } catch (e) {
        //   print("上班时间_onWorkTime()方法解析异常2：$e");
        // }
      }
    }
  }

  ///班次开始时间
  String formatOnWorkTime({String format = "MM-dd HH:mm"}) {
    try {
      _onWorkTime();
      return DateTimeUtils.format(_OnWorkTime, format: format);
    } catch (e) {
      print("班次上班时间格式解析异常：$e");
    }
    return OnWorkTime ?? "";
  }

  DateTime? _OffWorkTime = null;

  _offWorkTime() {
    if (_OffWorkTime == null && isNotEmpty(OffWorkTime)) {
      try {
        String? OffWorkTime2 = OffWorkTime?.replaceAll("/", "-");
        _OffWorkTime = DateTimeUtils.stringToDateTime(OffWorkTime2,
            format: "yyyy-MM-dd HH:mm");
      } catch (e) {
        print("班次下班时间_offWorkTime()方法解析异常：$e");
        // try {
        //   String? OffWorkTime2 = OffWorkTime?.replaceAll("/", "-");
        //   _OffWorkTime = DateTimeUtils.stringToDateTime(OffWorkTime2,
        //       format: "yyyy-MM-dd HH:mm");
        // } catch (e) {
        //   print("下班时间_offWorkTime()方法解析异常2：$e");
        // }
      }
    }
  }

  ///班次结束
  String formatOffWorkTime({String format = "MM-dd HH:mm"}) {
    try {
      _offWorkTime();
      return DateTimeUtils.format(_OffWorkTime, format: format);
    } catch (e) {
      print("班次下班时间格式解析异常：$e");
    }
    return OffWorkTime ?? "";
  }

  ///是否迟到
  bool isLate(DateTime? date) {
    if (date != null) {
      var time = DateTimeUtils.format(date, format: "HH:mm");
      var d = _time(time);
      var d2 = start();
      if (d > d2) {
        return true;
      }
    }
    return false;
  }

  ///是否早退
  bool isLeaveEarly(DateTime? date) {
    if (date != null) {
      var time = DateTimeUtils.format(date, format: "HH:mm");
      var d = _time(time);
      var d2 = end();
      if (d < d2) {
        return true;
      }
    }
    return false;
  }

  bool isSelected = false; //自己定义的参数，判断该班组是否选中。
  double difference = 0; //当前时间与班次开始时间差。
  ///计算,根据时间自动选择最近的班组
  ///time 当前小时时间；0.0~23.59; 0.0就是24.0；
  void time(double time) {
    var _start = start();
    var d = _start - time;
    difference = d.abs(); //取绝对值，即：负数变正数。
    if (time > _start && difference <= 3) {
      isSelected = true;
    } else if (time <= _start && difference <= 3) {
      isSelected = true;
    }
    if (!isSelected) {
      if (_start <= 2.0 && time >= 20) {
        isSelected = true; //晚班
      }
    }
  }

  ///字符串时间转换成数值时间，如："18:30" 转换成18.3
  ///"2022-04-07 8:00:00",
  double _time(String? time) {
    if (StringUtils.isNotEmpty(time)) {
      try {
        if (time!.contains("-") && time!.length > 5 && time!.contains(" ")) {
          //格式："2022-04-07 8:00:00"
          var times = time!.split(" ");
          time = times[1]; //取时分秒的部分。
        } else if (time!.contains("/") &&
            time!.length > 5 &&
            time!.contains(" ")) {
          //格式："2022-04-07 8:00:00"
          var times = time!.split(" ");
          time = times[1]; //取时分秒的部分。
        }
        //格式 "18:30"
        if (time!.contains(":")) {
          var times = time!.split(":");
          var t1 = double.parse(times[0]).toInt();
          var t2 = double.parse(times[1]).toInt();
          String t3 = t1.toString() + "." + t2.toString();
          return double.parse(t3);
        } else {
          return double.parse(time!);
        }
      } catch (e) {
        print("_time日期转换错误：$e");
      }
    }
    return 0;
  }

  ///获取班次开始时间
  double start() {
    return _time(OnWorkTime);
  }

  ///获取班次结束时间
  double end() {
    return _time(OffWorkTime);
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    AttendanceId = json["AttendanceId"]?.toString() ?? "";
    WorkTypeName = json["WorkTypeName"]?.toString() ?? "";
    OnWorkTime = json["OnWorkTime"]?.toString() ?? "";
    OffWorkTime = json["OffWorkTime"]?.toString() ?? "";
  }

  @override
  instance() {
    return MyAttendances();
  }

  @override
  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["AttendanceId"] = AttendanceId;
    map["WorkTypeName"] = WorkTypeName;
    map["OnWorkTime"] = OnWorkTime;
    map["OffWorkTime"] = OffWorkTime;
    return map;
  }
}
